<!doctype html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <script type="text/javascript">
       /* function getData(time,fn){
            //emulate ajax get data from server
            var data = new Date().getTime();
            var err;
            if(data%7 == 0){
                err = 'biz error:' + data;
            }
            setTimeout(function(){
                fn.call(this,err,data);
            },time*10);
        };

        getData(200,function(err,data){
            console.log('200s delay');
            getData(300,function(){
                console.log('300s delay');
                getData(100,function(){
                   console.log('100s delay');
                   console.log('end');
                });
            });
        });*/


        function getData2(time){
            //emulate ajax get data from server
            var data = new Date().getTime();
            var err;
            /*if(data%7 == 0){
                err = 'biz error:' + data;
            }*/
            return function(fn){
                console.log('getData2 callback call');
                setTimeout(function(){
                    fn.call(this,err,data);
                },time);
            }
        };

        function co(genFunc){
            return function(cb){
                debugger;
                console.log('begin');
                var gen = genFunc();
                var execNext = function(){
                    var next = gen.next();
                    var cbWarpper = next.value;
                    if(next.done != true){
                        cbWarpper(function(err,data){
                            cb.call(null,err,data);
                            execNext();
                        });
                    }
                };
                execNext();
            };
        }
        debugger;
        co(function* (){
            yield getData2(200);
            yield getData2(300);
            yield getData2(100);
        })(function(err,data){
            if(err){
                console.log('err:' + err);
            }else{
                console.log('data:' + data);
            }
        });

    </script>
</body>
</html>